package com.yangquan.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yangquan.entity.Student;
import com.yangquan.mapper.StudentMapper;
import com.yangquan.mapper.StudentUseMaper;
import com.yangquan.service.StudentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentUseMaper studentUseMaper;


    @Override
    public void addStudent(String students) {
        JSONObject jsonObject = JSONObject.parseObject(students);
        String str = jsonObject.getString("students");
        List<Student> studentsData = JSONArray.parseArray(str, Student.class);

        studentsData.stream().forEach(e ->
                studentMapper.insert(e));
//        studentMapper.insert(student);
    }

    @Override
    public void addStudentData(List<Student> students) {
        students.stream().forEach(e ->
                studentMapper.insert(e));

    }

    @Override
    @Cacheable(value = "REDIS_LOADMENU")
    public List<String> getStudentSalary() {
        return studentMapper.getStudentSalary();
    }

    @Override
    public List<Map<String,Object>> getStudent() {
        List<Map<String,Object>> student = studentUseMaper.getStudent();
        System.out.println(student+"  student");
        if(CollectionUtils.isNotEmpty(student)){
            System.out.println("包含 4");
        }

        List<Map<String,Object>> students = studentUseMaper.getStudentData();
        for (Map<String,Object> map : students) {
            System.out.println(map+"  map");
        }
        System.out.println(students+"  students");
        if(CollectionUtils.isNotEmpty(students)){
            System.out.println("包含 3");
        }
        return null;
    }

    @Override
    public List<Student> getStudentData() {
        List<Student> student = studentUseMaper.getStudentCeshi();
        System.out.println(student+"  student");
        if(CollectionUtils.isNotEmpty(student)){
            System.out.println("包含 4");
        }

        List<Student> students = studentUseMaper.getStudentDataCeshi();
        for (Student student1 : students) {
            System.out.println(student1+"  student1");
        }
        System.out.println(students+"  students");
        if(CollectionUtils.isNotEmpty(students)){
            System.out.println("包含 3");
        }
        return students;
    }

    @Override
    public List<Student> selectStudent() {
        return null;
    }

    @Override
    //allEntries=true 当指定了allEntries为true时，Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素，这比一个一个清除元素更有效率。
    @CacheEvict(value = "REDIS_LOADMENU",allEntries=true)
    public boolean updateStudent(String id) {
        System.out.println("传入的id是    "+id);
        return false;
    }


}
